<script setup>

import {pageofficeOpenFile} from "@/plugins/pageoffice/pageoffice-common.js";
import {backMyDocList} from "@/utils/backList.js";
import {isNull} from "@/utils/validate.js";
import AdviceList from "@/views/document/common/advice/adviceList.vue";
import modal from "@/plugins/modal.js";
import {filemakerctrl} from "js-pageoffice";
import CancelButton from "@/views/document/my/flowInfo/common/cancelButton.vue";
import {refreshParentRevisionCountFunction} from "@/plugins/pageoffice/pageoffice-word-excel.js";

const {proxy} = getCurrentInstance()
const loading = ref(false)
const emits = defineEmits(['loadMainViewData'])
const prop = defineProps({
  docId: {
    type: Number
  },
  revisionCount: {
    type: Number
  },
  flowInfoButtons: {
    type: JSON
  },
  isRefreshAdvice: {
    type: Boolean
  }
})
const revisionCount = ref(prop.revisionCount)
const { refreshRevisionCount } = refreshParentRevisionCountFunction(revisionCount)

function openApprovalFile() {
  pageofficeOpenFile('/document/my/common/openApprovalFile/' + prop.docId, prop.docId)
}

function openMainFile() {
  pageofficeOpenFile('/document/my/redSheet/openMainFile/' + prop.docId, prop.docId)
}

function openMainFileRed() {
  if (!isNull(revisionCount.value) && revisionCount.value > 0) {
    proxy.$modal.alertWarning('文件正文中尚有 ' + revisionCount.value + ' 条修订痕迹尚未处理，请清稿完毕后再执行当前操作')
    return false
  }
  pageofficeOpenFile('/document/my/redSheet/openMainFileRed/' + prop.docId, prop.docId)
}

function refreshMainViewData() {
  loading.value = false
  emits("loadMainViewData")
}

function handleNext() {
  if (!isNull(revisionCount.value) && revisionCount.value > 0) {
    proxy.$modal.alertWarning('文件正文中尚有 ' + revisionCount.value + ' 条修订痕迹尚未处理，请先处理完所有修订痕迹后再执行当前操作')
    return false
  }
  modal.confirm('确认已清稿套红完毕，进行盖章？').then(() => {
    modal.loading('正在执行，请稍后...')
    return mergeWordAndToPDF(prop.docId)
  })
}

function mergeWordAndToPDF(docId) {
  filemakerctrl.SaveFilePage = '/doc/my/redSheetCompleteWithSavePdf/' + docId
  filemakerctrl.CallFileMaker({
    url: "/doc/my/mergeAndToPdf/" + docId,
    success: function () {
      modal.closeLoading()
      refreshMainViewData()
    },
    error: function (msg) {
      modal.closeLoading()
      proxy.$modal.alertError(msg)
    }
  })
}

</script>

<template>
  <el-row v-loading="loading" element-loading-text="正在执行，请稍后...">
    <el-col :span="12" class="left">
      <el-row>
        <el-form-item>
          <el-button @click="backMyDocList">返 回</el-button>
          <el-button type="primary" @click="openApprovalFile">打开呈批表</el-button>
          <el-button type="primary" @click="openMainFile">正文清稿</el-button>
          <el-button type="primary" @click="openMainFileRed">正文套红</el-button>
          <el-button type="success" @click="handleNext" v-if="flowInfoButtons.isNext">清稿套红完毕</el-button>
        </el-form-item>
      </el-row>
    </el-col>
    <el-col :span="12">
      <el-row justify="end">
        <el-form-item>
          <CancelButton :docId="docId" :isCancel="flowInfoButtons.isCancel"/>
        </el-form-item>
      </el-row>
    </el-col>
  </el-row>

  <!-- 意见列表 -->
  <AdviceList :isRefreshAdvice="isRefreshAdvice"/>
</template>

